<script setup>

import {getCurrentInstance, ref} from "vue";
import {clearSysLoginLog, deleteSysLoginLog, listSysLoginLog} from "@/api/system/log/login_log";
import {loginLogStatusList} from "@/enum/enum";
import {enumToArray} from "@/utils/utils";

const {proxy} = getCurrentInstance();
const sysLoginLogList = ref([])
const total = ref(0)
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  loginLocation: null,
  userName: null,
  status: null,
  timeArr: null,
})
const ids = ref([])
const loginStatusOptions = enumToArray(loginLogStatusList);

const getList = async () => {
  const {data} = await listSysLoginLog(queryForm.value);
  total.value = data.total;
  sysLoginLogList.value = data.list;
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.logId);
}

const handleDelete = () => {
  const logId = ids.value;
  proxy.$modal.confirm(`确定删除选中的登录日志吗？`).then(async () => {
    await deleteSysLoginLog(logId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleClear = () => {
  proxy.$modal.confirm(`确定清空登录日志吗？`).then(async () => {
    await clearSysLoginLog();
    proxy.$modal.okNotify('清空成功');
    await getList();
  })
}

getList()

</script>

<template>
  <div class="app-container">

    <a-form :model="queryForm" ref="queryFormRef" layout="inline" class="demo-form-inline">
      <a-form-item label="登录地址" name="loginLocation">
        <a-input v-model:value="queryForm.loginLocation" placeholder="请输入登录地址"/>
      </a-form-item>
      <a-form-item label="用户名称" name="userName">
        <a-input v-model:value="queryForm.userName" placeholder="请输入用户名称"/>
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-select v-model:value="queryForm.status" placeholder="请选择状态">
          <a-select-option v-for="(item, index) in loginStatusOptions" :key="index" :label="item.label"
                           :value="item.value">{{ item.label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="登录时间" name="timeArr">
        <a-range-picker
            v-model:value="queryForm.timeArr"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            show-time
        />
      </a-form-item>
      <a-form-item>
        <a-space wrap>
          <a-button type="primary" @click="handleQuery" class="flex items-center mr-2">
            <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
            查询
          </a-button>
          <a-button plain @click="handleReset" class="flex items-center">
            <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
            重置
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>

    <a-space wrap class="!mt-4 !mb-4">
      <a-button danger plain :disabled="!ids.length" class="flex items-center"
                 @click="handleDelete" v-hasPermission="['system:login_log:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </a-button>
      <a-button danger plain class="flex items-center" @click="handleClear"
                 v-hasPermission="['system:login_log:clear']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        清空
      </a-button>
    </a-space>

    <a-table
      :data-source="sysLoginLogList"
      :border="true"
      size="large"
      row-key="logId"
      @selection-change="handleSelectionChange"
      :row-selection="{
        selectedRowKeys: ids,
        onChange: (selectedRowKeys) => {
          ids = selectedRowKeys;
        }
      }"
    >
      <a-table-column type="checkbox" width="55"/>
      <a-table-column data-index="logId" title="访问编号"/>
      <a-table-column data-index="userName" title="用户名称"/>
      <a-table-column data-index="ipAddr" title="登录地址"/>
      <a-table-column data-index="loginLocation" title="登录地点"/>
      <a-table-column data-index="browser" title="浏览器"/>
      <a-table-column data-index="os" title="操作系统"/>
      <a-table-column data-index="status" title="登录状态">
        <template #default="{record}">
          <a-tag :color="loginLogStatusList[record.status].type">{{ loginLogStatusList[record.status].label }}
          </a-tag>
        </template>
      </a-table-column>
      <a-table-column data-index="msg" title="操作信息">
        <template #default="{record}">
          <OverflowTooltip :content="record.msg"/>
        </template>
      </a-table-column>
      <a-table-column data-index="loginTime" title="登录日期" width="220"/>
    </a-table>

  </div>
</template>

<style scoped lang="scss">

</style>
